package com.faneui.transfer.gateway.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.faneui.transfer.dal.model.Cust;

public abstract class AbstractController {

	@Autowired
	private HttpServletRequest httpRequest;

	@Autowired
	protected HttpServletResponse httpResponse;

	@Autowired
	protected HttpSession httpSession;

	protected Cust getLoginCust(String ticket) {
		return (Cust) httpSession.getAttribute(ticket);
	}
	
	protected void keepLogin(String ticket,Cust cust) {
		httpSession.setAttribute(ticket, ticket);
	}

	protected void writeResponsePdfBytes(byte[] bytes, String filename) throws Exception {
		httpResponse.setDateHeader("Expires", 0L);
		httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		httpResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
		httpResponse.setHeader("Pragma", "no-cache");
		httpResponse.setContentType("application/octet-stream");
		httpResponse.setCharacterEncoding("UTF-8");
		String iso_filename = parseGBK(filename);
		httpResponse.setHeader("Content-Disposition", "attachment;filename=\"" + iso_filename + ".pdf\"");
		OutputStream os = httpResponse.getOutputStream();
		os.write(bytes, 0, bytes.length);
		os.flush();
	}

	protected void writeResponsePdfBytes(String filePath, String filename) throws Exception {
		this.setResponseFile(filename);
		// 以流的形式下载文件,读取jar包中的文件不能以路径的形形式读取
		// 获取输入流
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
		// 输出流
		BufferedOutputStream bos = new BufferedOutputStream(httpResponse.getOutputStream());
		byte[] buff = new byte[2048];
		int bytesRead;
		while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
			bos.write(buff, 0, bytesRead);
		}
		// 关闭流
		bis.close();
		bos.close();
	}

	protected void writeResponsePdfBytes(InputStream inputStream, String filename) throws Exception {
		this.setResponseFile(filename);
		// 获取输入流
		BufferedInputStream bis = new BufferedInputStream(inputStream);
		// 输出流
		BufferedOutputStream bos = new BufferedOutputStream(httpResponse.getOutputStream());
		byte[] buff = new byte[2048];
		int bytesRead;
		while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
			bos.write(buff, 0, bytesRead);
		}
		// 关闭流
		bis.close();
		bos.close();
	}

	public static String parseGBK(String sIn) {
		try {
			return java.net.URLEncoder.encode(sIn, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	protected void setResponseFile(String filename) {
		httpResponse.setDateHeader("Expires", 0L);
		httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		httpResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
		httpResponse.setHeader("Pragma", "no-cache");
		httpResponse.setContentType("application/octet-stream");
		httpResponse.setCharacterEncoding("UTF-8");
		String iso_filename = parseGBK(filename);
		httpResponse.setHeader("x-filename", iso_filename);
		httpResponse.setHeader("Content-Disposition", "attachment;filename=\"" + iso_filename + "\"");
	}

}
